﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;

namespace Meraz.Core.Model
{
    public class TweetValues
    {
        public TweetValues()
        {
            Links = new List<string>();
            Mentions = new List<TwitterMention>();
            Tags = new List<string>();
            InReplyToScreenName = "";
        }
        const string PROP_FORMAT = @"{0}: '{1}'";
        const string DATETIME_PATTERN = @"\w\w\w\s(?<MONTH>\w\w\w)\s(?<DAY>\d\d)\s(?<TIME>[\d:]+)\s(?:\+[\d]*\s)?(?<YEAR>\d\d\d\d)";
        static Regex DATETIME_REGEX = new Regex(DATETIME_PATTERN, RegexOptions.CultureInvariant);




        public string InReplyToScreenName { get; set; }
        public string InReplyToStatusID { get; set; }
        public string InReplyToUserID { get; set; }
        public string Retweeted { get; set; }
        public string Source { get; set; }
        public long UserID { get; set; }
        public string Username { get; set; }
        public long TweetID { get; set; }
        public string Text { get; set; }
        public string Published { get; set; }
        public DateTime Pubdate { get; set; }
        public string ClientName { get; set; }
        public TwitterClient Client { get; set; }

        public List<string> Links { get; set; }
        public List<TwitterMention> Mentions { get; set; }
        public List<string> Tags { get; set; }

        public void SetTweetID(string str)
        {
            long val = 0;
            if (long.TryParse(str, out val))
                TweetID = val;
            else
                throw new ApplicationException("improper TweetID string");
        }
        public void SetUserID(string str)
        {
            long val = 0;
            if (long.TryParse(str, out val))
                UserID = val;
            else
                throw new ApplicationException("improper UserID string");
        }
        public void SetPubdate(string datestring)
        {
            if (string.IsNullOrWhiteSpace(datestring))
                throw new ApplicationException("improper Pubdate string");

            StringBuilder dateBuilder = new StringBuilder();
            Match match = DATETIME_REGEX.Match(datestring);
            if (match.Success)
            {
                dateBuilder.AppendFormat("{0} ", match.Groups["MONTH"].Value);
                dateBuilder.AppendFormat("{0}, ", match.Groups["DAY"].Value);
                dateBuilder.AppendFormat("{0} ", match.Groups["YEAR"].Value);
                dateBuilder.AppendFormat("{0}", match.Groups["TIME"].Value);
                //Debug.WriteLine(string.Format("dateBuilder: '{0}'", dateBuilder));
                DateTime result;
                if (DateTime.TryParse(dateBuilder.ToString(), out result))
                {
                    this.Pubdate = result;
                    return;
                }
            }
            throw new ApplicationException(string.Format("improper Pubdate string: '{0}', StringBuilder: '{1}'", datestring, dateBuilder));
        }


        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine();
            sb.AppendLine(string.Format(PROP_FORMAT, "TweetID", TweetID));
            sb.AppendLine(string.Format(PROP_FORMAT, "InReplyToScreenName", InReplyToScreenName));
            sb.AppendLine(string.Format(PROP_FORMAT, "InReplyToStatusID", InReplyToStatusID));
            sb.AppendLine(string.Format(PROP_FORMAT, "InReplyToUserID", InReplyToUserID));
            sb.AppendLine(string.Format(PROP_FORMAT, "Retweeted", Retweeted));
            sb.AppendLine(string.Format(PROP_FORMAT, "Source", Source));
            sb.AppendLine(string.Format(PROP_FORMAT, "UserID", UserID));
            sb.AppendLine(string.Format(PROP_FORMAT, "Text", Text));
            sb.AppendLine(string.Format(PROP_FORMAT, "Pubdate", Pubdate));
            sb.AppendLine(string.Format(PROP_FORMAT, "ClientName", ClientName));
            sb.AppendLine("Links:");
            foreach (string link in Links)
                sb.AppendLine(link);

            sb.AppendLine("Tags:");
            foreach (string tag in Tags)
                sb.AppendLine(tag);

            sb.AppendLine("Mentions:");
            foreach (TwitterMention mention in Mentions)
                sb.AppendLine(mention.ScreenName);
            sb.AppendLine();
            return sb.ToString();
        }
    }
}
